Given two strings s and t , write a function to determine if t is an anagram of s.
判斷s是否為t的重組字。
Input: s = "anagram", t = "nagaram"
Output: true
Input: s = "rat", t = "car"
Output: false
若為重組字,代表它們的內容一定相等,只是字元位置不同,
宣告一個長度26元素都為0的陣列,使用charCode取得字元的 AtUnicode 編碼,s的 AtUnicode 編碼負責在陣列對應的位置上+1,t負責-1,
若內容相等,最終會抵消全為0。
var isAnagram = function (s, t) {
if (s.length !== t.length) { return false; }
const charSet = new Array(26).fill(0);
for (let i = 0; i < s.length; i++) {
charSet[s.charCodeAt(i) - 97]++;
charSet[t.charCodeAt(i) - 97]--;
}
for (let i = 0; i < charSet.length; i++) {
if (charSet[i] !== 0) { return false; }
}
return true;
};